<?


JCAT_Package::ImportClass( ROOT_PATH.'Classes/Coder/class.CoderDocBase.php') ;

class ControllerDoc extends CoderDocBase implements IJCATMVCObject
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$sPath=''	string	What's this Parameter ?
	 * @return	array
	 */
	public function ControllerDoc($sPath='')
	{
		$this->CoderDocBase( __CLASS__, $sPath ) ;
		
		// 初始化文档结构  :::::::::;
		// 所拥有的 子控制器/文档/视图
		$aSubDoc = new JCATMVCObjectContainerDoc('MVCChildren') ;
		$this->AddDocument($aSubDoc,'MVCChildren') ;
	}
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjectClass()
	{
		return 'JCAT_Controller' ;
	}
	
	static public function GetControllerClassName()
	{
		return 'Controller' ;
	}
	
	/**
	 * What's this Method ?
	 * 
	 * @access	protected
	 * @return	void
	 */
	protected function CreateNewDocument()
	{
		// 创建 新的文件 内容 :::::::::::::::::::
		$aTemplate = new JCAT_UIHtml() ;
		$aTemplate->SetVar('TheDoc',$this) ;
		
		// 模版文件
		$sTemplateFile = dirname(__FILE__).'/template.Controller.php' ;
	
		// 产生内容
		$sNewSource = $aTemplate->Display( $sTemplateFile,false) ;
		
		// 调用 父类 创建一个空文件
		parent::CreateNewDocument() ;
		
		// 写入新文件
		file_put_contents($this->GetPath(),$sNewSource) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjctName()
	{
		return $this->Get('ControllerName') ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetKindIcon($nSize=16)
	{
		return ROOT_PATH.'Resource/Image/Controller.24x24.png' ;
	}
	
	public function GetObjectChildrenDoc()
	{
		return $this->GetDocument('MVCChildren') ;
	}
	
	
	
	// 创建代码 /////////////////////////////////////////////
	
	
	/**
	 * 将序列化以后的内容存入到 文档
	 * 
	 * @access	protected
	 * @param	$sPath				文件路径
	 * @param	$sByteContents		字节内容
	 * @param	$bAsBinary			作为二进制存储
	 * @return	bool
	 */
	protected function SaveToFile( $sPath, $sByteContents, $bAsBinary=false )
	{
		// 调用父类
		parent::SaveToFile( $sPath, $sByteContents, $bAsBinary ) ;

		foreach ($this->arrCodeBlocks as $sCodeBlockName=>$nTabNum)
		{
			$sMethodName = 'CreateCode_'.str_replace(' ','',$sCodeBlockName) ;
			CoderDocBase::WirteUnmodifiableBlock($sPath,$sCodeBlockName,$this->$sMethodName(),CoderDocBase::UnmodifiableBlockKind_Code,$nTabNum) ;
		}

		// 完成 返回 
		return true ;
	}


	protected function CreateCode_MessageQueue()
	{
		if( !$this->Get('UseMsgQueue') )
		{
			return '' ;
		}
		
		$sTab = str_repeat("\t",2) ;
		
		// 消息队列 类型 ------------------------------
		$arrCodeLines[] = "{$sTab}// ".JCAT_Language::Sentence('消息队列') ; 
		$arrCodeLines[] = "{$sTab}\$aMessageQueue = new JCAT_MessageQueue() ;" ;

		$arrMsgTypes = $this->Get('MsgType') ;
		foreach ($arrMsgTypes as $arrMsg)
		{
			if(!$arrMsg)
			{
				continue ;
			}
			
			$arrCodeLines[] = "{$sTab}JCAT_MessageDispatcher::RegisterMessageQueue(\$aMessageQueue,'{$arrMsg[MsgType::CLM_MSGTYPE]}') ;" ;
		}
		
		// 消息队列的输出器 -----------------------------
		$arrCodeLines[] = '' ; 
		$arrCodeLines[] = "{$sTab}// ".JCAT_Language::Sentence('消息输出对象') ; 		
		$arrMsgPriters = $this->Get('MsgPriter') ;
		foreach ($arrMsgPriters as $arrPrinters)
		{
			if( !$arrPrinters[MsgPriter::CLM_USE] )
			{
				continue ;
			}
			
			$sPrinterClassName = $arrPrinters[MsgPriter::CLM_PRINTER_CLASS] ;
			$arrCodeLines[] = "{$sTab}\$aMessageQueue->AddPrinter(new {$sPrinterClassName}()) ;" ; 		
		}
		
		
		$arrCodeLines[] = '' ; 
		$arrCodeLines[] = "{$sTab}\$this->SetMessageQueue(\$aMessageQueue) ;" ;
		
		return implode("\r\n",$arrCodeLines) ;
	}
	
	protected function CreateCode_InitializeChildren()
	{
		$arrCodes = array() ;
		
		$aDoc = $this->GetObjectChildrenDoc() ;
		$arrMVCObjects = $aDoc->Get('Children') ;
		
		// 文档 和 子控制器
		foreach ($arrMVCObjects as $arrObject)
		{			
			switch ($arrObject[TreeJCATMVCObject::CLM_TYPE])
			{
				case 'JCAT_IController' :
					
					$arrCodes[] = $this->CreateCode_InitializeController(&$arrObject) ;
					
					break ;
					
				case 'JCAT_IDocument' :
					
					$arrCodes[] = $this->CreateCode_InitializeDocument(&$arrObject) ;
					
					break ;
					
				case 'JCAT_IView' :
					
					$arrCodes[] = $this->CreateCode_InitializeView(&$arrObject) ;
					
					break ;
					
				default:
					break ;
			}
			
		}
		
		// 返回
		return implode("\r\n\r\n",$arrCodes) ;
	}
	
	protected function CreateCode_RegisterChildrenGlobalName()
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		
		$aMVCContainerView = JCAT_View::GetViewByGlobalName('JCATMVCObjectContainerView') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aMVCContainerView,'JCATMVCObjectContainerView') ;

		$aIterator = $aMVCContainerView->CreateMVCObjectIterator() ;
		$aModel = $aIterator->GetModel() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aModel) ;
		
		// 开始迭代
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aItem = $aIterator->Current() ;
			
			$sGlobalName = trim($aModel->get_value($aItem,TreeJCATMVCObject::CLM_GLOBAL_NAME)) ;
			
			if($sGlobalName)
			{
				$sGlobalName = str_replace(';',',',$sGlobalName) ;
				$arrGlobalNames = explode(',',$sGlobalName) ;
				
				$sName = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_NAME) ;
				$sType = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_TYPE) ;
				$sType = preg_replace('/^JCAT_I/','',$sType) ;
				
				// 对象名称
				$sVariableName = "\$a{$sType}" ;
				
				// 间隔行
				if( count($arrCodeLines) )
				{
					$arrCodeLines[] = "\r\n" ;
				}
				
				// 创建 代码
				$arrCodeLines[] = "{$sTab}{$sVariableName} = JCAT_{$sType}::Get{$sType}ByGlobalName('{$sName}') ;" ;
				foreach ($arrGlobalNames as $sOneGlobalName)
				{
					$arrCodeLines[] = "{$sTab}JCAT_{$sType}::RegisterGlobalName({$sVariableName},'{$sOneGlobalName}') ;" ;
				}
			}
			
			$aIterator->Next() ;
		}
		
		// 返回
		if( count($arrCodeLines) )
		{
			return "{$sTab}// " . JCAT_Language::Sentence('注册全局对象') . "\r\n". implode("\r\n",$arrCodeLines) ;
		}
		
		else
		{
			return '' ;
		}
	}
	
	// 为 View 设置 文档
	protected function CreateCode_SetDocumentToView()
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		
		$aMVCContainerView = JCAT_View::GetViewByGlobalName('JCATMVCObjectContainerView') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aMVCContainerView,'JCATMVCObjectContainerView') ;

		$aIterator = $aMVCContainerView->CreateMVCObjectIterator() ;
		$aModel = $aIterator->GetModel() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aModel) ;
		
		// 开始迭代
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aItem = $aIterator->Current() ;

			$sType = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_TYPE) ;
			$sDocName = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_DOCNAME) ;
			
			if( $sType=='JCAT_IView' and $sDocName )
			{
				$sName = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_NAME) ;
				$arrCodeLines[] = "{$sTab}JCAT_Controller::SetDocumentToView('{$sDocName}','{$sName}') ;" ;
			}

			$aIterator->Next() ;
		}
	}
	
	/**
	 * what's this
	 * 
	 * @access	private
	 * @return	void
	 */
	private function CreateCode_InitializeController(array $arrControllerInfo)
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
				
		$sName = $arrControllerInfo[TreeJCATMVCObject::CLM_NAME] ;
		$sPath = $arrControllerInfo[TreeJCATMVCObject::CLM_PATH] ;
		$sGlobalName = trim($arrControllerInfo[TreeJCATMVCObject::CLM_GLOBAL_NAME]) ;
		
		$arrCodeLines[] = "{$sTab}// ".JCAT_Language::Sentence('创建并加载子控制器：%s',$sName) ;
		$arrCodeLines[] = "{$sTab}JCAT_Package::ImportClass('{$sName}',\$sHere.'{$sPath}');" ;
		$arrCodeLines[] = "{$sTab}\$aSubController = new {$sName}(\$this) ;" ;

		$arrCodeLines[] = "{$sTab}\$this->AddController(\$aSubController) ;" ;
		
		
		return implode("\r\n",$arrCodeLines) ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	private
	 * @return	void
	 */
	private function CreateCode_InitializeDocument(array $arrControllerInfo)
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		
		$sName = $arrControllerInfo[TreeJCATMVCObject::CLM_NAME] ;
		$sPath = $arrControllerInfo[TreeJCATMVCObject::CLM_PATH] ;
		$sGlobalName = trim($arrControllerInfo[TreeJCATMVCObject::CLM_GLOBAL_NAME]) ;

		$arrCodeLines[] = "{$sTab}// ".JCAT_Language::Sentence('创建并加载文档：%s',$sName) ;
		$arrCodeLines[] = "{$sTab}JCAT_Package::ImportClass('{$sName}',\$sHere.'{$sPath}');" ;
		$arrCodeLines[] = "{$sTab}\$aDocument = new {$sName}() ;" ;
		
		// 文档 需要手工 设置全局名称
		$arrCodeLines[] = "{$sTab}JCAT_Document::RegisterGlobalDocument(\$aDocument,'{$sName}') ;" ;

		$arrCodeLines[] = "{$sTab}\$this->AddDocument(\$aDocument,\$aDocument->GetName()) ;" ;

		return implode("\r\n",$arrCodeLines) ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	private
	 * @return	void
	 */
	private function CreateCode_InitializeView(array $arrControllerInfo)
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		
		$sName = $arrControllerInfo[TreeJCATMVCObject::CLM_NAME] ;
		$sPath = $arrControllerInfo[TreeJCATMVCObject::CLM_PATH] ;
		$sGlobalName = trim($arrControllerInfo[TreeJCATMVCObject::CLM_GLOBAL_NAME]) ;
		$sDocName = trim($arrControllerInfo[TreeJCATMVCObject::CLM_DOCNAME]) ;

		$arrCodeLines[] = "{$sTab}// ".JCAT_Language::Sentence('创建并加载视图：%s',$sName) ;
		$arrCodeLines[] = "{$sTab}JCAT_Package::ImportClass('{$sName}',\$sHere.'{$sPath}');" ;
		$arrCodeLines[] = "{$sTab}\$aView = new {$sName}(\$this) ;" ;
		
		// 设置文档
		if($sDocName)
		{
			$arrCodeLines[] = "{$sTab}\$aDocument = JCAT_Document::GetDocumentByGlobalName('{$sDocName}') ;" ;
			$arrCodeLines[] = "{$sTab}JCAT_ASSERT::ASSERT_INSTANCE(\$aDocument,'JCAT_IDocument',JCAT_Language::Sentence('没有定义名为：%s 的文档。','{$sDocName}')) ;" ;
			$arrCodeLines[] = "{$sTab}\$aView->SetDocument(\$aDocument) ;" ;
		}
		
		$arrCodeLines[] = "{$sTab}\$this->AddView(\$aView) ;" ;

		return implode("\r\n",$arrCodeLines) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function CreateCode_MainRun()
	{
		$arrCodes = array() ;
		
		$aDoc = $this->GetObjectChildrenDoc() ;
		$arrMVCObjects = $aDoc->Get('Children') ;
		
		$sThisDir = dirname($this->GetPath()) ;
		$sThisDir = JCAT_Global::TidyPath($sThisDir) ;
		
		// 文档 和 子控制器
		foreach ($arrMVCObjects as $arrObject)
		{
			if( !$arrObject[TreeJCATMVCObject::CLM_ACTIVE] )
			{
				continue ;
			}
			
			$sRelativePath = $arrObject[TreeJCATMVCObject::CLM_PATH] ;
			$sPath = $sThisDir.$sRelativePath ;
		
			// 恢复 子控件 的文档
			$aDocument = CoderDocBase::WakeupDocument($sPath) ;
			if( $aDocument )
			{
				JCAT_ASSERT::ASSERT_INSTANCE($aDocument,'IJCATMVCObject') ;	
				$arrCodes[] = $aDocument->CreateCode_ForControllerMainRun() ;		
			}
		}
		
		return implode("\r\n\r\n",$arrCodes) ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	string
	 */
	public function CreateCode_ForControllerMainRun()
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		
		$sName = $this->GetName() ;
		
		$arrCodeLines[] = "{$sTab}// " . JCAT_Language::Sentence('执行子控制器  %s',$sName) ;
		$arrCodeLines[] = "{$sTab}\$aChildController = \$this->GetController('{$sName}') ;" ;
		$arrCodeLines[] = "{$sTab}JCAT_ASSERT::ASSERT_INSTANCE(\$aChildController,'JCAT_IController',JCAT_Language::Sentence('名为 %s 的子控制器未定义','{$sName}')) ;" ;
		
		$arrCodeLines[] = "{$sTab}\$aChildController->MainRun(& \$arrArg) ;" ;

		return implode("\r\n",$arrCodeLines) ;
	}

	private $arrCodeBlocks = array(
				'Message Queue' => 2
				, 'Initialize Children' =>2
				, 'Register Children Global Name' =>2
				, 'Set Document To View' =>2
	) ;
}

JCAT_Package::ImportClass('JCAT_UIHtml',JCAT::PACKAGE_UIHTML) ;
?>